package com.wakaka.controller;

import com.wakaka.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerController {
    //消费者不应该有service层
    //REST FUL风格 使用RestTemplate 供我们直接调用

    @Autowired
    RestTemplate template;  //提供多种便捷访问远程http服务的方法，简单的restful服务模板


    // Ribbon 这里的地址应该是一个变量，通过服务地址来访问
    //    private static final String URL_PREFIX = "http://localhost:8081";
    private static final String URL_PREFIX = "http://PROVIDER";

    @RequestMapping("/consumer/get/{id}")
    public Dept getDeptById(@PathVariable("id") long id) {
        return template.getForObject(URL_PREFIX + "/queryById/" + id, Dept.class);
    }

    @RequestMapping("/consumer/add/{name}")
    public int add(@PathVariable("name") String name) {
        return template.postForObject(URL_PREFIX + "/add", name, Integer.class);
    }

    @RequestMapping("/consumer/queryAll")
    public List<Dept> queryAll() {
        return template.getForObject(URL_PREFIX + "/queryAll", List.class);
    }
}
